// 获取所有的app
// http://app.tpvstore.com/tvapi/v1/apps
const { pick } = require('lodash');
const { apps } = require('../model/index');
const commonData = require('../lib/commonData').base;

const getData = async ctx => {
    console.log(ctx.request.href);
    const category = ctx.query.id;
    let list = await apps.find(category ? { category } : {});
    list = list.map((app, id) => {
        const el = Object.assign({
            description: app.name,
            // path: 'http://app.tpvstore.com/' + app.apkPath,
            path: app.dlLink,
        }, commonData, app.toObject(), { logoBackground: app.sshots[0] || '', id });
        return pick(el, ["dwnCount", "icon", "id", "logoBackground", "md5hash", "name", "package", "path", "supportTv", "vercode", "vername"]);
    });
    ctx.type = 'text/html;charset=UTF-8';
    ctx.body = JSON.stringify({
        count: list.length,
        listing: list,
    })
};

module.exports = function () {
    this.all('/tvapi/v1/apps', getData);

    this.all('/tvapi/v1/apps/category', getData);
}